package com.shimakaze.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shimakaze.reggie.common.CustomException;
import com.shimakaze.reggie.dto.SetmealDto;
import com.shimakaze.reggie.entity.Dish;
import com.shimakaze.reggie.entity.Setmeal;
import com.shimakaze.reggie.entity.SetmealDish;
import com.shimakaze.reggie.mapper.SetmealMapper;
import com.shimakaze.reggie.service.DishService;
import com.shimakaze.reggie.service.SetmealDishService;
import com.shimakaze.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Lazy
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时保存套餐和菜品的关联信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息，对setmeal执行insert
        this.save(setmealDto);

        //保存菜品和套餐的关联信息。对setmeal_dish执行insert
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }


    /**
     * 删除套餐，同时删除套餐和菜品的关联信息
     * 需要操作 setmeal和setmeal_dish 两张表
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态确定是否可以删除。不能则抛出业务异常
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(lambdaQueryWrapper);
        if(count > 0){
            throw new CustomException("所选套餐中有正在售卖套餐,无法删除");
        }

        //先删除关系表的数据 setmeal_dish
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper_1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper_1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper_1);

        //再套餐表中的数据 setmeal
        this.removeByIds(ids);

    }

    /**
     * //根据id查询套餐信息和关联菜品，需要操作两张表：setmeal、dish
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDishes(Long id) {
        //查询套餐的基本信息
        Setmeal setmeal = this.getById(id);

        if(setmeal != null){
            //将套餐基本信息拷贝到SetmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);

            //从setmeal_dish查询套餐关联的菜品
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);

            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);

            return setmealDto;
        }
        return null;
    }


    /**
     * 修改套餐信息，同时修改setmeal、setmeal_dish两张表
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateByIdWithDishes(SetmealDto setmealDto) {

        //修改套餐: setmeal的基本信息
        this.updateById(setmealDto);

        /**
         * 更新setmeal_dish表
         * 先删除菜品 : setmeal_dish表的delete操作
         * 再添加提交过来的菜品 : setmeal_dish表的insert操作
         */
        //删除
        LambdaQueryWrapper<SetmealDish> setmealDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDtoLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDtoLambdaQueryWrapper);
        //添加
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishList);

    }


    /**
     * 批量修改套餐状态
     * 如果一个套餐被起售，那么其关联的所有菜品都必须起售
     * @param status
     * @param ids
     */
    @Transactional
    @Override
    public void updateStatus(int status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(ids!=null,Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal,setmealLambdaQueryWrapper);

        //如果一个套餐被起售，那么其关联的所有菜品都必须起售
        if(status == 1){
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);

            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);

/*            for(SetmealDish setmealDish : setmealDishList){
                Long dishId = setmealDish.getDishId();
                Dish dish = dishService.getById(dishId);
                dish.setStatus(1);
                dishService.updateById(dish);
            }*/

            setmealDishList.stream().map((item) -> {
               Long dishId = item.getDishId();
               Dish dish = dishService.getById(dishId);
               dish.setStatus(1);
               dishService.updateById(dish);

               return setmealDishList;

            }).collect(Collectors.toList());
        }

    }
}
